package com.example.carprovideruser.service.impl.hkf;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.carinterface.pojo.User;
import com.example.carinterface.service.hkf.UserService;
import com.example.carinterface.utils.ResultJson;
import com.example.carprovideruser.mapper.hkf.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public Integer updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public ResultJson selectMyCoupon(Integer userId) {
        List<Map> myCoupon = new ArrayList<>();
        myCoupon=userMapper.selectMyCoupon(userId);
        ResultJson<Object> resultJson = new ResultJson<>();
        resultJson.setSuccess("查询成功",myCoupon);
        return resultJson;
    }

    @Override
    public ResultJson insertMyCoupon(Map<String, Object> map) {

        ResultJson<Object> resultJson = new ResultJson<>();
        if(userMapper.insertMyCoupon(map)>0){
           if(userMapper.updateexchangeCode(map)>0&&userMapper.updateCoupon(map)>0){return resultJson.setSuccess("领取成功");}
        }

        return resultJson.setFail("领取失败,优惠劵已发送完");
    }

    @Override
    @Transactional
    public ResultJson selectCouponUserCode(Map<String, Object> map) {
       Map<String, Object> coupon = userMapper.selectCouponUserCode(map);

        ResultJson<Object> resultJson = new ResultJson<>();

        if(coupon!=null){
            coupon.put("userId",(Integer)map.get("userId"));

            if ((Integer)coupon.get("couponNum")>=(Integer)coupon.get("exchangeNum")){
                if (userMapper.selectCouponInMyCoupon(coupon)==null){
                if(userMapper.insertMyCoupon(coupon)>0){
                    if(userMapper.updateexchangeCode(coupon)>0&&userMapper.updateCoupon(coupon)>0){return resultJson.setSuccess("领取成功");}
                }
            }else {
                   if (userMapper.updateMyCoupon(coupon)>0){
                       if(userMapper.updateexchangeCode(coupon)>0&&userMapper.updateCoupon(coupon)>0){return resultJson.setSuccess("领取成功");}
                   }
                }}else {
                return resultJson.setFail("该优惠劵已抢光了");
            }
        }

            return resultJson.setFail("无效兑换码");

    }

    @Override
    public ResultJson selectMyBusinessOrder(Map<String, Object> map) {
        ResultJson resultJson=new ResultJson();
        List<Map> list=userMapper.selectMyBusinessOrder(map);
        return resultJson.setSuccess("成功",list);
    }

    @Override
    public ResultJson selectBanner() {
        ResultJson resultJson=new ResultJson();
       List<Map> list= userMapper.selectBanner();
        return resultJson.setSuccess("成功",list);
    }

    @Override
    public ResultJson dianjiBanner(Map<String, Object> map) {
        ResultJson resultJson=new ResultJson();
       if (userMapper.dianjiBanner(map)>0){
        return resultJson.setSuccess("已增加点击量");}
        return resultJson.setFail("失败");
    }

    @Override
    public ResultJson selectUser(Map<String, Object> map) {
        ResultJson resultJson=new ResultJson();
        Map<String, Object> user=  userMapper.selectUser(map);
        return resultJson.setSuccess("成功",user);
    }

}
